package com.bi.donghua;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView= (ImageView) findViewById(R.id.iv);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"wwwww",Toast.LENGTH_SHORT).show();

            }
        });

    }

    public void MyClick(View view) {
       /* TranslateAnimation translateAnimation=new TranslateAnimation(0,100,0,0);
        translateAnimation.setRepeatCount(Animation.INFINITE);
        translateAnimation.setRepeatMode(Animation.REVERSE);

        RotateAnimation rotateAnimation=new RotateAnimation(0,180,Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF);
        rotateAnimation.setRepeatMode(Animation.REVERSE);
        rotateAnimation.setRepeatCount(Animation.INFINITE);

        AnimationSet set=new AnimationSet(true);
        set.addAnimation(translateAnimation);
        set.addAnimation(rotateAnimation);
        set.setDuration(2000);
        imageView.startAnimation(set);*/

        //属性动画的执行顺序控制
        // 先同步执行动画animator1和animator3,然后再执行animator2
        //set.play(animator3).with(animator1);
        //set.play(animator2).after(animator3);
        //  ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView, "translationY", 0F, 300F);//Y轴平移旋转

        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels; // 屏幕宽度（像素）



        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", 0F, width);//X轴平移旋转
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 180F);//180度旋转

        /*animator1.setRepeatMode(ValueAnimator.REVERSE);
        animator1.setRepeatCount(ValueAnimator.INFINITE);
        animator3.setRepeatMode(ValueAnimator.REVERSE);
        animator3.setRepeatCount(ValueAnimator.INFINITE);*/

        AnimatorSet set = new AnimatorSet();
        set.play(animator3).with(animator1);
        set.setDuration(3000);
        set.start();
    }

    public void MyClick2(View view) {
        DisplayMetrics metric = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metric);
        int width = metric.widthPixels; // 屏幕宽度（像素）



        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "translationX", width,0);//X轴平移旋转
        ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView, "rotation", 0F, 180F);//180度旋转

        /*animator1.setRepeatMode(ValueAnimator.REVERSE);
        animator1.setRepeatCount(ValueAnimator.INFINITE);
        animator3.setRepeatMode(ValueAnimator.REVERSE);
        animator3.setRepeatCount(ValueAnimator.INFINITE);*/

        AnimatorSet set = new AnimatorSet();
        set.play(animator3).with(animator1);
        set.setDuration(3000);
        set.start();

    }
}
